﻿using Microsoft.AspNetCore.Mvc;
using System.Data;
using tools.office.npoi;

namespace tools.api.Controllers
{
    /// <summary>
    /// NPOI 工具操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class NpoiController : ControllerBase
    {
        [HttpGet]
        public IActionResult Import(string filePath)
        {
            return Ok(NpoiExcelImportTool.GetDataTable(filePath));
        }

        [HttpGet]
        public IActionResult FastExport()
        {
            string[] titles = ["序号", "全球号", "图号", "品名", "规格描述", "裸件在库", "成品在库", "净重G", "毛重G", "辅材信息"];
            var fs = NpoiExcelExportTool.FastExport(GetMockData(), titles);
            return File(fs, "application/octet-stream", "待包装裸件.xlsx");
        }

        [HttpGet]
        public IActionResult HandmadeExport()
        {
            DataTable dataTable = GetMockData();

            var typeDict = new Dictionary<int, string>
            {
                { 1, "纸箱" },
                { 2, "泡沫" },
                { 3, "胶带" },
                { 4, "说明书" }
            };

            var fs = NpoiExcelExportTool.HandmadeExport(dataTable, typeDict, "C23");
            return File(fs, "application/octet-stream", "待包装裸件.xlsx");
        }

        [HttpGet]
        public IActionResult TemplateExport()
        {
            DataTable dataTable = GetMockData();

            var typeDict = new Dictionary<int, string>
            {
                { 1, "纸箱" },
                { 2, "泡沫" },
                { 3, "胶带" },
                { 4, "说明书" }
            };

            var fs = NpoiExcelExportTool.TemplateExport(dataTable, typeDict, "C23");
            return File(fs, "application/octet-stream", "待包装裸件.xlsx");
        }

        [HttpGet]
        public IActionResult SimpleExport()
        {
            var fs = NpoiExcelExportTool.SimpleExport(GetMockData(), "C23");
            return File(fs, "application/octet-stream", "待包装裸件.xlsx");
        }

        private static DataTable GetMockData()
        {
            DataTable dataTable = new();

            dataTable.Columns.Add("number", typeof(int));                  // 序号
            dataTable.Columns.Add("productnumber", typeof(string));        // 全球号
            dataTable.Columns.Add("daikin_drawingno", typeof(string));     // 图号
            dataTable.Columns.Add("name", typeof(string));                 // 品名
            dataTable.Columns.Add("new_specification", typeof(string));    // 规格描述
            dataTable.Columns.Add("daikin_check_accept", typeof(int));     // 裸件在库
            dataTable.Columns.Add("finished_products", typeof(int));       // 成品在库
            dataTable.Columns.Add("daikin_wt", typeof(double));            // 净重G
            dataTable.Columns.Add("daikin_capacitywt", typeof(double));    // 毛重G
            dataTable.Columns.Add("bc_infos", typeof(string));             // 包装信息/辅材信息

            DataRow row1 = dataTable.NewRow();
            row1["number"] = 1;
            row1["productnumber"] = "G123456789";
            row1["daikin_drawingno"] = "DRW-001";
            row1["name"] = "压缩机";
            row1["new_specification"] = "220V/50Hz/1.5HP";
            row1["daikin_check_accept"] = 100;
            row1["finished_products"] = 50;
            row1["daikin_wt"] = 12.5;
            row1["daikin_capacitywt"] = 15.2;
            row1["bc_infos"] = "纸箱+泡沫";
            dataTable.Rows.Add(row1);

            DataRow row2 = dataTable.NewRow();
            row2["number"] = 2;
            row2["productnumber"] = "G987654321";
            row2["daikin_drawingno"] = "DRW-002";
            row2["name"] = "热交换器";
            row2["new_specification"] = "铜管+铝翅片/300×200×50mm";
            row2["daikin_check_accept"] = 75;
            row2["finished_products"] = 25;
            row2["daikin_wt"] = 8.3;
            row2["daikin_capacitywt"] = 10.1;
            row2["bc_infos"] = "木箱+防震材料";
            dataTable.Rows.Add(row2);

            DataRow row3 = dataTable.NewRow();
            row3["number"] = 3;
            row3["productnumber"] = "G555555555";
            row3["daikin_drawingno"] = "DRW-003";
            row3["name"] = "电机";
            row3["new_specification"] = "DC24V/100W";
            row3["daikin_check_accept"] = 200;
            row3["finished_products"] = 80;
            row3["daikin_wt"] = 5.7;
            row3["daikin_capacitywt"] = 7.8;
            row3["bc_infos"] = "纸盒+塑料袋";
            dataTable.Rows.Add(row3);

            return dataTable;
        }
    }
}
